//
//  WBTitleButton.swift
//  swiftweibo
//
//  Created by ffxk007 on 2017/6/22.
//  Copyright © 2017年 ffxk007. All rights reserved.
//

import UIKit

class WBTitleButton: UIButton {
    
     /// 重载构造函数
     ///
     /// - Parameter title: <#title description#>
    //如果是nil就显示首页，不是nil显示title和箭头图像
    init(title: String?) {
        super.init(frame:CGRect())
        //判断title是否为nil
        if title == nil {
            setTitle("首页", for: [])
        }else {
            setTitle(title! + " ", for: [])
           
            //FIXME:设置图像没实现
        }
        
        //设置字体和颜色
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        setTitleColor(UIColor.darkGray, for: [])
        
         sizeToFit()

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //判断lable和iamgeview是否同时存在
        guard let titleLabel = titleLabel,let imageView = imageView else {
            return
        }
        
        //将label的X向左移动imageview的宽度
        titleLabel.frame = titleLabel.frame.offsetBy(dx: -imageView.bounds.width, dy: 0)
        //将iamgeview的X像右移动lable的宽度
        imageView.frame = imageView.frame.offsetBy(dx: titleLabel.bounds.width, dy: 0)
    }
}
